package cn.zhp.service.impl;

import cn.zhp.entity.SysResource;
import cn.zhp.mapper.SysResourceMapper;
import cn.zhp.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysResourceServiceImpl extends BaseServiceImpl<SysResource> implements SysResourceService {


    @Autowired
    SysResourceMapper sysResourceMapper;


    /**
     * 根据角色id查询当前角色的所有resource
     * 和系统全部resource
     * @param rid
     * @return
     */
    @Override
    public Map<String,Object> selectByRid(long rid){
        HashMap<String, Object> map = new HashMap<>();
        SysResource sysResource = new SysResource();
        sysResource.setDelFlag("0");
        map.put("all",sysResourceMapper.select(sysResource));
        map.put("resources",sysResourceMapper.selectByRid(rid));
        return map;
    }

    /**
     * 根据用户id查询其所有资源，并且去重
     * @param uid
     * @return
     */
    @Override
    public List<SysResource> selectByUid(long uid) {
        List<SysResource> sysResources = sysResourceMapper.selectByUid(uid);
//        HashSet<SysResource> resourceSet = new HashSet<>();
        //1.散列计算得到散列桶索引位置   2.在索引位置上逐一比较桶中的对象
//        resourceSet.addAll(sysResources);//必须重写equals才能保证去重
        return sysResources;
    }


    /**
     * 查询所有url不为‘’的菜单资源
     * @return
     */
    @Override
    public List<SysResource> selectResources(){
        return sysResourceMapper.selectResources();
    }
}
